package uabc.arduino.componentes;

import java.awt.BorderLayout;

import javax.swing.ButtonGroup;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.util.ArrayList;

import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.JButton;

import uabc.arduino.elementos.Sensor;
import uabc.arduino.elementos.SerieConfiguration;
import uabc.arduino.elementos.TipoSensor;
import uabc.arduino.interfaces.Constantes;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class DialogAddConfiguracion extends JDialog implements Constantes {

	private final JPanel contentPanel = new JPanel();
	private boolean acepto;
	private JTextField txtfIdentificador;
	private JTextField txtfNombre;
	private JComboBox cmbxTipoSensor;
	private JComboBox cmbxSensor;
	private JTextField txtA;
	private JTextField txtB;
	private JTextField txtC;
	
	private TipoSensor[] tiposSensores;

	/**
	 * Create the dialog.
	 */
	public DialogAddConfiguracion(TipoSensor[] tipoSensores) {
		setBounds(100, 100, 400, 280);
		this.tiposSensores = tipoSensores;
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);

		JButton btnCancelar = new JButton("Cancelar");
		btnCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
			}
		});
		btnCancelar.setBounds(265, 194, 89, 23);
		contentPanel.add(btnCancelar);

		JButton btnAceptar = new JButton("Aceptar");
		btnAceptar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (!txtfIdentificador.getText().trim().equals("")
						|| !txtfNombre.getText().trim().equals("")
						|| !txtA.getText().trim().equals("")
						|| !txtB.getText().trim().equals("")
						|| !txtC.getText().trim().equals("")) {
					try {
						Float.parseFloat(txtA.getText());
						Float.parseFloat(txtB.getText());
						Float.parseFloat(txtC.getText());
						acepto = true;
						setVisible(false);
					} catch (NumberFormatException ex) {
						JOptionPane.showMessageDialog(contentPanel,
								"Favor de capturar bien los datos de pendiete");
					}
				} else {
					JOptionPane.showMessageDialog(contentPanel,
							"Capture todos los datos", "Error",
							JOptionPane.INFORMATION_MESSAGE);
				}
			}
		});
		btnAceptar.setBounds(166, 194, 89, 23);
		contentPanel.add(btnAceptar);

		JLabel lblNombre = new JLabel("Nombre:");
		lblNombre.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lblNombre.setBounds(63, 67, 100, 20);
		contentPanel.add(lblNombre);

		JLabel lblIdentificador = new JLabel("Identificador:");
		lblIdentificador.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lblIdentificador.setBounds(63, 42, 100, 20);
		contentPanel.add(lblIdentificador);

		JLabel lblTipoDeSensor = new JLabel("Tipo de Sensor:");
		lblTipoDeSensor.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lblTipoDeSensor.setBounds(63, 117, 100, 20);
		contentPanel.add(lblTipoDeSensor);

		JLabel lbl = new JLabel("Ecuacion:");
		lbl.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lbl.setBounds(63, 92, 100, 20);
		contentPanel.add(lbl);

		JLabel lblSensor = new JLabel("Sensor:");
		lblSensor.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lblSensor.setBounds(63, 142, 100, 20);
		contentPanel.add(lblSensor);

		txtfIdentificador = new JTextField();
		txtfIdentificador.setBounds(166, 42, 150, 20);
		contentPanel.add(txtfIdentificador);
		txtfIdentificador.setColumns(10);

		txtfNombre = new JTextField();
		txtfNombre.setColumns(10);
		txtfNombre.setBounds(166, 67, 150, 20);
		contentPanel.add(txtfNombre);

		cmbxTipoSensor = new JComboBox(tipoSensores);
		cmbxTipoSensor.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				TipoSensor selected = (TipoSensor) cmbxTipoSensor
						.getSelectedItem();
				cmbxSensor.removeAllItems();
				for (Sensor s : selected.getSensores())
					cmbxSensor.addItem(s);
			}
		});
		cmbxTipoSensor.setBounds(166, 117, 150, 20);
		contentPanel.add(cmbxTipoSensor);

		cmbxSensor = new JComboBox(tipoSensores[0].getSensores().toArray());
		cmbxSensor.setBounds(166, 142, 150, 20);
		contentPanel.add(cmbxSensor);

		txtA = new JTextField();
		txtA.setBounds(166, 92, 40, 20);
		contentPanel.add(txtA);
		txtA.setColumns(10);

		txtB = new JTextField();
		txtB.setColumns(10);
		txtB.setBounds(220, 93, 40, 20);
		contentPanel.add(txtB);

		txtC = new JTextField();
		txtC.setColumns(10);
		txtC.setBounds(276, 93, 40, 20);
		contentPanel.add(txtC);
	}

	public boolean isAcepto() {
		return acepto;
	}

	public void setAcepto(boolean acepto) {
		this.acepto = acepto;
	}

	public SerieConfiguration getConfiguracion() {
		SerieConfiguration configuracion = new SerieConfiguration();
		configuracion.setNombre(txtfNombre.getText().trim());
		configuracion.setID(txtfIdentificador.getText().trim());
		configuracion.setSensor(cmbxSensor.getSelectedItem().toString());
		try {
			configuracion.setA(Float.parseFloat(txtA.getText()));
			configuracion.setB(Float.parseFloat(txtB.getText()));
			configuracion.setC(Float.parseFloat(txtC.getText()));
		} catch (NumberFormatException e) {
		}
		return configuracion;
	}
}
